package com.app.community.controller;

import com.app.community.dto.PersonDTO;
import com.app.community.exception.ErrorCodeEnum;
import com.app.community.model.entity.User;
import com.app.community.service.AvatarService;
import com.app.community.service.PersonService;
import com.wordnik.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

/**
 * @Date 2020/10/28 22:27
 **/
@Slf4j
@Controller
public class PersonController {
    @Autowired
    AvatarService avatarService;

    @Autowired
    PersonService personService;

    @Autowired
    HttpSession httpSession;

    @ApiOperation("本人访问")
    @GetMapping("person")
    public String getPerson( Model model) {
        String userId;

        // 已登录
        if ((userId = (String) httpSession.getAttribute("userId")) != null && !userId.isEmpty()) {
            // 本人访问信号量设为2
            model.addAttribute("signal", 2);
            PersonDTO personDTO = personService.selectQuestionAndUser(userId);
            addUserInfo(personDTO.getUser(), model, true);
            // 用户发表的问题
            model.addAttribute("questions", personDTO.getQuestionList());
            return "common/person";
        } else {
            model.addAttribute("signal",1);
            model.addAttribute("error", ErrorCodeEnum.HAVENOTLOGIN.getDescription());
            return "common/person";
        }


    }

    @ApiOperation("他人和本人都可访问")
    @GetMapping(path = "person/{userId}")
    public String getPerson( Model model, @PathVariable("userId") String userId) {
        PersonDTO personDTO = personService.selectQuestionAndUser(userId);
//        本人访问和非本人访问页面内容会有所不同
        // 如果是本人访问
        if (userId.equals(httpSession.getAttribute("userId"))) {
            // 本人访问信号量设为2
            model.addAttribute("signal", 2);
            // 添加个人信息
            addUserInfo(personDTO.getUser(), model, true);
        } else { // 如果不是本人访问

            addUserInfo(personDTO.getUser(), model, false);
        }

        // 用户发表的问题
        model.addAttribute("questions", personDTO.getQuestionList());
        return "common/person";
    }


    /**
     * 有个问题就是要确保是本人操作
     */
    @PostMapping("delete")
    public String deleteQuestionAndComment(@RequestParam("questionId") Long questionId, HttpSession
            httpSession, Model model) {

        // 删除问题和评论,userId用于确认本人操作
        personService.deleteQuestionAndComment(questionId, (String) httpSession.getAttribute("userId"));

        PersonDTO personDTO = personService.selectQuestionAndUser((String) httpSession.getAttribute("userId"));
        model.addAttribute("questions", personDTO.getQuestionList());
        return "common/person::questionsFragment";
    }

    /**
     * 避免代码冗余，添加用户信息
     * boolean用于判断是否为本人,true为本人操作，false非本人操作
     */
    private void addUserInfo(User user, Model model, Boolean flag) {
        // 添加当前用户头像
        model.addAttribute("currentAvatar", avatarService.selectUserAvatar((String)httpSession.getAttribute("userId")));

        if (user == null) {
            if (flag) {
                model.addAttribute("nickName", "您还没有个人资料哦，快去完善吧");
            } else {
                model.addAttribute("nickName", "这个用户还没有个人资料哦，提醒他完善吧");
            }
        } else {
            model.addAttribute("avatar", user.getAvatar());
            model.addAttribute("nickName", user.getNickName());
            model.addAttribute("coverPic", user.getCoverPic());
            model.addAttribute("introduction", user.getIntroduction());
        }
    }
}
